import 'package:flutter/material.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_utils/holiday_theme.dart';

class HomeSortHeader extends StatefulWidget {
  final Function(int sort) sortCallBack;
  final List<String>? tabs;
  final Function(bool style)? changePageStyleCallback;
  final bool showChangePageStyle;
  final EdgeInsets? labelPadding;
  final TabAlignment? tabAlignment;
  final bool isInTop; //
  final Widget? rightWidget;
  final Color? bgColor;
  const HomeSortHeader({
    super.key,
    required this.sortCallBack,
    this.changePageStyleCallback,
    this.showChangePageStyle = true,
    this.tabs,
    this.labelPadding,
    this.tabAlignment,
    this.isInTop = false,
    this.rightWidget,
    this.bgColor,
  });

  @override
  State<HomeSortHeader> createState() => _SortHeaderState();
}

class _SortHeaderState extends State<HomeSortHeader> with SingleTickerProviderStateMixin {
  late List<String> tabs;
  late final TabController _controller;

  @override
  void initState() {
    super.initState();
    tabs = widget.tabs ?? ['热门推荐', '最新上架', '最多观看', '最多收藏'];
    _controller = TabController(length: tabs.length, vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Color bgColor = widget.bgColor ?? Theme.of(context).scaffoldBackgroundColor;
    if (holidayTheme.openHoliday && !widget.isInTop) {
      bgColor = Colors.transparent;
    }
    return Container(
      height: 38,
      decoration: BoxDecoration(
        color: bgColor,
        gradient: (widget.isInTop && holidayTheme.openHoliday)
            ? LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Color(0xff3e161c),
                  Color(0xff26161c),
                ],
              )
            : null,
      ),
      child: _buildMenuView(),
    );
  }

  Widget _buildMenuView() {
    if (widget.rightWidget != null) {
      return Row(
        children: [Expanded(child: _buildTabBar()), widget.rightWidget!],
      );
    }
    return _buildTabBar();
  }

  Widget _buildTabBar() {
    return TabBar(
      tabs: tabs.map((e) => Text(e)).toList(),
      controller: _controller,
      isScrollable: true,
      tabAlignment: widget.tabAlignment ?? TabAlignment.center,
      onTap: (index) => widget.sortCallBack.call(index),
      indicator: BoxDecoration(),
      indicatorWeight: 0,
      labelPadding: widget.labelPadding ?? EdgeInsets.symmetric(horizontal: 8),
      unselectedLabelStyle: TextStyle(
        color: Color(0x73FFFFFF),
        fontSize: 14,
      ),
      labelStyle: TextStyle(
        color: Color(0xE5ffffff),
        fontSize: 14,
        fontWeight: FontWeight.w600,
      ),
    );
  }
}

class SortDarkHeader extends StatefulWidget {
  final Function(int sort) sortCallBack;
  final EdgeInsets? labelPadding;
  final TabAlignment? tabAlignment;
  final Color? backgroundColor;

  const SortDarkHeader({
    super.key,
    required this.sortCallBack,
    this.labelPadding,
    this.tabAlignment,
    this.backgroundColor,
  });

  @override
  State<StatefulWidget> createState() {
    return _SortDarkHeaderState();
  }
}

class _SortDarkHeaderState extends State<SortDarkHeader> with SingleTickerProviderStateMixin {
  late final TabController _controller;

  //视频类型条件选中图片
  static List<String> darkSelectImage = [
    "type1_select.webp".darkwebPath,
    "type2_select.webp".darkwebPath,
    "type3_select.webp".darkwebPath,
    "type4_select.webp".darkwebPath,
  ];

  //视频类型条件未选中图片
  static List<String> darkNormalImage = [
    "type1_normal.webp".darkwebPath,
    "type2_normal.webp".darkwebPath,
    "type3_normal.webp".darkwebPath,
    "type4_normal.webp".darkwebPath,
  ];

  @override
  void initState() {
    super.initState();
    _controller = TabController(initialIndex: 0, length: darkNormalImage.length, vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.backgroundColor ?? Theme.of(context).scaffoldBackgroundColor,
      child: TabBar(
        tabs: List.generate(darkNormalImage.length, (index) {
          if (index == _controller.index) {
            return Image.asset(darkSelectImage[index], height: 20);
          }
          return Image.asset(darkNormalImage[index], height: 20);
        }),
        controller: _controller,
        isScrollable: true,
        tabAlignment: widget.tabAlignment ?? TabAlignment.center,
        onTap: (index) {
          widget.sortCallBack.call(index);
          setState(() {});
        },
        indicator: BoxDecoration(),
        indicatorWeight: 0,
        labelPadding: widget.labelPadding ?? EdgeInsets.symmetric(horizontal: 10),
        unselectedLabelStyle: TextStyle(
          color: Color(0xFF999999),
          fontSize: 15,
        ),
        labelStyle: TextStyle(
          color: Color(0xffffffff),
          fontSize: 15,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }
}
